package codeRandomThoughts.Test349两个数组的交集;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        //题目中规定 0 <= nums1[i], nums2[i] <= 1000
        int[] same = new int[1001];
        Set<Integer> res = new HashSet<>();
        for (int i : nums1) {
            same[i]++;
        }
        for (int i : nums2) {
            same[i]--;
            if (same[i] >= 0) {
                res.add(i);
            }
        }
        int[] result = new int[res.size()];
        List<Integer> collect = res.stream().collect(Collectors.toList());
        for (int i = 0; i < collect.size(); i++) {
            result[i] = collect.get(i);
        }
        return result;
    }
}
